﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThreeByte.ActiveDeck.Data;
using log4net;
using System.Text.RegularExpressions;
using ThreeByte.ActiveDeck.Service;

namespace ThreeByte.ActiveDeck.Web.console
{
    public partial class ResetPassword : System.Web.UI.Page
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(ResetPassword));

        protected void Page_Load(object sender, EventArgs e) {

            //if(Page.IsPostBack) {
            //    //Don't do anything on PostBack refesh
            //    return;
            //}

            SubmitButton.Attributes.Add("onmouseover", "this.src='./skins/images/Submit_on.png'");
            SubmitButton.Attributes.Add("onmouseout", "this.src='./skins/images/Submit_off.png'");

            try {
                string resetKey = Request.QueryString["x"];
                key = Guid.Parse(resetKey);

                if(DataUtil.IsPasswordResetRequestValid(key)) {
                    //Valid
                    return;
                } else {
                    //This link has expired
                }
            } catch(Exception ex) {
                log.Error("Error verifying password reset", ex);
            }

            InfoLabel.Visible = false;
            ForgotLinkTable.Visible = true;
            ResultHeader.Text = "Invalid Link";
            Page.Header.Title = "Invalid Link";
            SubmitTable.Visible = false;
        }

        private Guid key;

        protected void SubmitButton_Click(object sender, ImageClickEventArgs e) {

            //Clear Errors
            NewPasswordTextBox.BorderWidth = 0;
            ConfirmPasswordTextBox.BorderWidth = 0;
            EmailTextBox.BorderWidth = 0;
            ErrorLabel.Text = null;

            //Get the email address
            string emailAddress = EmailTextBox.Text;
            
            //Test Email address
            string validEmail = DataUtil.GetEmailForPasswordResetKey(key);
            if((validEmail == null)
                || (emailAddress != validEmail)) {
                    EmailTextBox.BorderWidth = 3;
                    ErrorLabel.Text = "Incorrect email address";
                    EmailTextBox.Focus();
                    return;
            }

            //Confirm the passwords match
            if(string.IsNullOrWhiteSpace(NewPasswordTextBox.Text)) {
                //New password cannot be empty
                NewPasswordTextBox.BorderWidth = 3;
                ErrorLabel.Text = "New password cannot be empty";
                NewPasswordTextBox.Focus();
                return;
            }

            if(NewPasswordTextBox.Text != ConfirmPasswordTextBox.Text) {
                //New password cannot be empty
                NewPasswordTextBox.BorderWidth = 3;
                ConfirmPasswordTextBox.BorderWidth = 3;
                ErrorLabel.Text = "Passwords do not match";
                NewPasswordTextBox.Focus();
                return;
            }

            
            SubmitTable.Visible = false;

            string newPassword = NewPasswordTextBox.Text;

            UserPasswordUtil.ResetPassword(emailAddress, newPassword);
            DataUtil.RecordPasswordReset(key);

            //Change the UI to reflect
            ResultLabel.Text = "Your password has been changed successfully.";
            
        }
    }
}